﻿using Abp.BackgroundJobs;
using Abp.Dependency;
using Abp.Domain.Repositories;
using Abp.Domain.Uow;
using Mt.Site.Core.Navigations.Entities;
using Mt.Site.Core.Synchronization;
using Mt.Site.Core.Synchronization.Data;
using Mt.Site.Core.Synchronization.Navigations;
using System;
using System.Threading.Tasks;

namespace Mt.Site.Core.BackGroundJobs.Navigations
{
    public class SynchorizationMenuConfigJob : AsyncBackgroundJob<BaseAndMenuIdArgs>, ITransientDependency
    {
        private readonly IMenuInfoAutoSyncManager _menuInfoAutoSyncManager;

        private readonly IBackgroundJobManager _backgroundJobManager;

        private readonly ISynchronizationHelper _synchronizationHelper;

        private readonly IRepository<MenuConfig, Guid> _menuConfigRepository;

        public SynchorizationMenuConfigJob(
            IMenuInfoAutoSyncManager menuInfoAutoSyncManager,
            IBackgroundJobManager backgroundJobManager,
            ISynchronizationHelper synchronizationHelper,
            IRepository<MenuConfig, Guid> menuConfigRepository)
        {
            _menuInfoAutoSyncManager = menuInfoAutoSyncManager;
            _backgroundJobManager = backgroundJobManager;
            _synchronizationHelper = synchronizationHelper;

            _menuConfigRepository = menuConfigRepository;
        }

        [UnitOfWork]
        public override async Task ExecuteAsync(BaseAndMenuIdArgs args)
        {
            using (CurrentUnitOfWork.DisableFilter(
                AbpDataFilters.MayHaveTenant,
                AbpDataFilters.MustHaveTenant,
                MtSiteConsts.AbpDataFilters.MustHaveLanguage,
                AbpDataFilters.SoftDelete))
            {
                var sonSites = _synchronizationHelper.GetSonSite(args.FromTenantId, args.FromLanguage, args.MenuId);

                var fromEntity = _menuConfigRepository.FirstOrDefault(args.Id);
                if (fromEntity == null)
                {
                    return;
                }

                foreach (var item in sonSites)
                {
                    if (!item.AutoSynchronous) //栏目不接受同步
                    {
                        continue;
                    }

                    var toEntityId = Guid.Empty;
                    if (args.OperateType == OptionType.Create || args.OperateType == OptionType.Update)
                    {
                        var createOrUpdateSyncStore = new CreateOrUpdateSynchorizeStore
                        {
                            Id = args.Id,
                            TargetLanguage = item.TargetLanguage,
                            ToTenantId = item.TargetTenantId,
                            OptionType = args.OperateType
                        };

                        toEntityId = await _menuInfoAutoSyncManager.UpdateConfigSync(createOrUpdateSyncStore);
                    }

                    if (toEntityId != Guid.Empty)
                    {
                        var nextSites = _synchronizationHelper.GetSonSite(item.TargetTenantId, item.TargetLanguage, args.MenuId);
                        if (nextSites.Count > 0)
                        {
                            _backgroundJobManager.Enqueue<SynchorizationMenuConfigJob, BaseAndMenuIdArgs>(
                                    new BaseAndMenuIdArgs
                                    {
                                        FromLanguage = item.TargetLanguage,
                                        FromTenantId = item.TargetTenantId,
                                        Id = toEntityId,
                                        MenuId = args.MenuId,
                                        OperateType = args.OperateType
                                    });
                        }
                    }
                }
            }
        }
    }
}
